_unit_dead_pos = _this select 0
DeathCamCurCamTarget setPos _unit_dead_pos
zigo_HeightDummy setPos _unit_dead_pos

;~2.5
;titleCut ["","BLACK OUT", 1.5]
;~2
;titleCut ["","BLACK IN", 4]

DeathCamMissileCamEnabled = true
DeathCamCineViewSpecial = 0
DeathCamComSpeed = 0.4
DeathCamRelPos = [0,-2,0.3]
DeathCamTarHeight = 1.7
DeathCamIndex = 0

DeathCamCurTarget = player
DeathCam = "camera" camCreate [(_unit_dead_pos select 0)+2, (_unit_dead_pos select 1)+2, 700 ]
DeathCam cameraEffect ["internal","front"]
DeathCam camSetTarget vehicle DeathCamCurCamTarget
DeathCam camSetPos [(_unit_dead_pos select 0)+2, (_unit_dead_pos select 1)+2, 1000 ]
DeathCam camSetFOV 0.976
DeathCam camCommand "inertia on"
DeathCam camCommit 0
showCinemaBorder false

titleCut ["","BLACK IN", 4]
#start

_ok = createDialog "auto_revive_by_Zigo_v1_4_0_RscFDFDlgSpectateDeath"
ctrlShow [1046001,false];ctrlShow [1046002,false];ctrlShow [1046003,false];ctrlShow [1046004,false];ctrlShow [1046005,false];ctrlShow [1046006,false];ctrlShow [1046007,false];ctrlShow [1046008,true];ctrlShow [1046009,false];ctrlShow [1046010,false];ctrlShow [1046011,false];ctrlShow [1046012,false];ctrlShow [1046013,false];ctrlShow [1046014,false];ctrlShow [1046015,false];ctrlShow [1046016,false];ctrlShow [1046017,false];ctrlShow [1046018,false];ctrlShow [1046019,false];ctrlShow [1046020,false];ctrlShow [1046021,false];ctrlShow [1046022,false];ctrlShow [1046023,false]
ctrlSetText [116969,name player]
ctrlShow [1047002,false];ctrlShow [1047001,false];ctrlShow [1048001,false]

sliderSetRange [1046010, 0.2, 7] 
sliderSetPosition [1046010, 1] 


kegfoo = 0
"_cur_player_ent = call _x; _i = lbAdd [1047002, PlayableNames_zigo select kegfoo];lbSetData [1047002, _i, format[""%1"",kegfoo]];if (side _cur_player_ent == west) then {lbSetColor[1047002, _i, [0.8,0.8,1,0.8]]};if (side _cur_player_ent == EAST) then {lbSetColor[1047002, _i, [1,0.8,0.8,0.8]]};if (side _cur_player_ent == resistance) then {lbSetColor[1047002, _i, [0.8,1,0.8,0.8]]};kegfoo = kegfoo + 1" foreach PlayableUnits_zigo
lbSetCurSel [1047002, 0] 

kegfoo = 0
"if (player == (call _x)) then {lbSetCurSel [1047002, kegfoo]; DeathCamIndex = kegfoo};kegfoo = kegfoo + 1;" foreach PlayableUnits_zigo



? not _ok : exit

#loop
_target_was_changed_due_to_unspectability =false
; Check revived status
?PlayerWasRevived: goto "player_was_revived"

; Is player spectatable ?
_spectatable_entity = DeathCamIndex call function_determine_if_player_is_spectatable
?not (isNull _spectatable_entity): goto "found_spectatable"

; Is body bag spectatable ?
_spectatable_entity =  DeathCamIndex call function_determine_if_body_bag_is_spectatable
?not (isNull _spectatable_entity): goto "found_spectatable"

; No spectatable found, need change current target
~0.5
DeathCamIndex = DeathCamIndex + 1
_target_was_changed_due_to_unspectability = true
?DeathCamIndex >= PlayableUnitsCount_zigo: DeathCamIndex = 0
goto "loop"

#found_spectatable
DeathCamCurTarget = _spectatable_entity

?_target_was_changed_due_to_unspectability && dialog: 0 exec "auto_revive_by_Zigo_v1_4_0_nextCam.sqs"

kegfoo = 0
"_color_check_entity = kegfoo call function_determine_if_player_is_spectatable; if (isNull _color_check_entity) then {lbSetColor[1047002, kegfoo, [0,0,0,0.4]]} else {if (side _color_check_entity == west) then {lbSetColor[1047002, kegfoo, [0.8,0.8,1,0.8]]};if (side _color_check_entity == EAST) then {lbSetColor[1047002, kegfoo, [1,0.8,0.8,0.8]]};if (side _color_check_entity == resistance) then {lbSetColor[1047002, kegfoo, [0.8,1,0.8,0.8]]}} ;kegfoo = kegfoo + 1" foreach PlayableUnits_zigo

? lbData[1047002,(lbCurSel 1047002)] != format["%1",DeathCamIndex] : _TargetChange = 1
? _TargetChange == 1 : DeathCamIndex = lbCurSel 1047002
? (_TargetChange == 1) and (dialog): 0 exec "auto_revive_by_Zigo_v1_4_0_nextCam.sqs"
_TargetChange = 0

? !dialog : goto "seagull"

DeathCam camCommit DeathCamComSpeed
_vhlTemp = vehicle DeathCamCurTarget

if !(_vhlTemp in DeathCamEventHandledVehicles) then {DeathCamEventHandledVehicles = DeathCamEventHandledVehicles + [_vhlTemp];_vhlTemp addEventHandler["fired",{if ((_this select 0) == (vehicle DeathCamCurTarget)) then {_this exec "auto_revive_by_Zigo_v1_4_0_missileCam.sqs"}}]}


; Cinematic view?
? DeathCamCineViewSpecial == 1 : goto "camspecial"

; 1st Person view?
? DeathCamCineViewSpecial == 3 : goto "camspecial1st"

; Gunner view?
? DeathCamCineViewSpecial == 4 : goto "camspecialgunnerview"

; The following will reduce the effect of warping spectated object to the camera itself

_Divider = 100 - (speed _vhlTemp * 2)
? _Divider < 10 : _Divider = 10

_moX = (getpos DeathCamCurCamTarget select 0) + (((getpos _vhlTemp select 0) - (getpos DeathCamCurCamTarget select 0))/_Divider)
_moY = (getpos DeathCamCurCamTarget select 1) + (((getpos _vhlTemp select 1) - (getpos DeathCamCurCamTarget select 1))/_Divider)
_moZ = ((getpos DeathCamCurCamTarget select 2)-DeathCamTarHeight) + (((getpos _vhlTemp select 2) - ((getpos DeathCamCurCamTarget select 2)-DeathCamTarHeight))/_Divider)

; Workaround for an ofp "feature" with the setpos/getpos commands and roadway lods
zigo_HeightDummy setpos [getpos _vhlTemp select 0,getpos _vhlTemp select 1,0]
_h2 = _vhlTemp distance zigo_HeightDummy
_h1 = _h2 + DeathCamTarHeight
? vehicle DeathCamCurTarget == DeathCamCurTarget : _moZ = ((_h1)-DeathCamTarHeight) + (((_h2) - ((_h1)-DeathCamTarHeight))/_Divider)

DeathCamCurCamTarget setpos[_moX,_moY,_moZ  + DeathCamTarHeight]

DeathCamCurCamTarget setdir getdir _vhlTemp

; Top view?
? DeathCamCineViewSpecial == 2 : goto "camspecialTop"

_RealRelPos = [(DeathCamRelPos select 0) * (sliderPosition 1046010), (DeathCamRelPos select 1) * (sliderPosition 1046010), (DeathCamRelPos select 2) * ((sliderPosition 1046010)*2)]

? ((speed vehicle DeathCamCurTarget >= 150)) : _RealRelPos = [(DeathCamRelPos select 0) * (sliderPosition 1046010) * 5, (DeathCamRelPos select 1) * (sliderPosition 1046010) * 5, (DeathCamRelPos select 2) * ((sliderPosition 1046010)*2) * 5]

DeathCam camSetRelPos _RealRelPos 



goto "loop"


; Cinematic view
#camspecial
DeathCamCurCamTarget setpos[getpos _vhlTemp select 0,getpos _vhlTemp select 1,(getpos _vhlTemp select 2) + DeathCamTarHeight]
DeathCamCurCamTarget setdir getdir _vhlTemp

; Reset cam if object is far away... if its a very fast object use a more distant view
? ((speed vehicle DeathCamCurTarget >= 150)) and (((DeathCam) distance (DeathCamCurCamTarget)) > 400) : DeathCam camSetRelPos [3,400.1,10.0]
? ((speed vehicle DeathCamCurTarget < 150)) and (((DeathCam) distance (DeathCamCurCamTarget)) > 100) : DeathCam camSetRelPos DeathCamRelPos

; Distance slider sets zoom (FOV) in cinematic view
DeathCam camSetFOV 0.976 * ((sliderPosition 1046010)/5)

goto "loop"

; Top view
#camspecialTop
DeathCamCurCamTarget setdir 0

DeathCam camSetPos [getpos DeathCamCurCamTarget select 0,getpos DeathCamCurCamTarget select 1,(getpos DeathCamCurCamTarget select 2)+(30 * sliderPosition 1046010 * 1.5)]

goto "loop"


; User pressed esc, wait 5 seconds
#seagull

titleCut ["","BLACK IN", 0]

~5
; Return back to spectating

titleCut ["","BLACK OUT", 0.5]
~0.5
titleCut ["","BLACK IN", 2]
DeathCam cameraEffect ["internal","front"]
goto "start"

#camspecial1st

closedialog 821151551
titleCut ["","BLACK IN", 1]
~0.1
_ok = createDialog "auto_revive_by_Zigo_v1_4_0_RscFDFDlgSpectateDeath1st"

hint " Press ESC to return "

ctrlSetText [116969,PlayableNames_zigo select DeathCamIndex]

#camspecial1stloop

~0.001
(vehicle DeathCamCurTarget) switchCamera "INTERNAL"
(vehicle DeathCamCurTarget) cameraEffect ["terminate","FRONT"]

_tmp_spec_unit_is_spectatable = not (isNull (DeathCamIndex call function_determine_if_player_is_spectatable))
? (DeathCamCineViewSpecial == 3) && (dialog) && (not PlayerWasRevived) && _tmp_spec_unit_is_spectatable: goto "camspecial1stloop"
DeathCamCineViewSpecial = 0

titleCut ["","BLACK IN", 1]
closedialog 821151552
~0.1

DeathCam switchCamera "INTERNAL"
DeathCam cameraEffect ["internal","front"]

goto "start"

#camspecialgunnerview

closedialog 821151551
titleCut ["","BLACK IN", 1]
~0.1
_ok = createDialog "auto_revive_by_Zigo_v1_4_0_RscFDFDlgSpectateDeath1st"

hint " Press ESC to return "

ctrlSetText [116969,PlayableNames_zigo select DeathCamIndex]

#camspecialgunnerviewloop

~0.001
(vehicle DeathCamCurTarget) switchCamera "GUNNER"
(vehicle DeathCamCurTarget) cameraEffect ["terminate","BACK"]

_tmp_spec_unit_is_spectatable = not (isNull (DeathCamIndex call function_determine_if_player_is_spectatable))
? (DeathCamCineViewSpecial == 4) && (dialog) && (not PlayerWasRevived) && _tmp_spec_unit_is_spectatable: goto "camspecialgunnerviewloop"
DeathCamCineViewSpecial = 0

titleCut ["","BLACK IN", 1]
closedialog 821151552
~0.1

DeathCam switchCamera "INTERNAL"
DeathCam cameraEffect ["internal","front"]

goto "start"

#player_was_revived
titlecut ["","Plain down"]

enableRadio true
;? (DeathCamCineViewSpecial != 3) and (DeathCamCineViewSpecial != 4): closedialog 821151551; goto "all_views_cam_exit"
;closedialog 821151552
;DeathCam cameraEffect ["internal","front"]
;#all_views_cam_exit

closedialog 821151551
DeathCam camSetTarget player
DeathCam camSetRelPos [10.49, 0.61, 3.48]
DeathCam camSetFov 0.7
DeathCam camCommit 5
DeathCam camCommand "manual off"
@camCommitted DeathCam

DeathCam camSetRelPos [-0.82, -3.12, 3.38]
DeathCam camSetFov 0.044
DeathCam camCommit 3.5
@camCommitted DeathCam

#exitcam
player switchCamera "INTERNAL"
player cameraEffect ["Terminate", "BACK"]
camDestroy DeathCam

exit
